package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.home.adapter.ExerciseRecordListAdapter;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.bean.ExerciseRecordListBean;
import com.shushan.pages.bean.ExerciseRecordTotalBean;
import com.shushan.pages.bean.LastExerciseRecordBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: ExerciseRecordActivity
 * @Desciption: //练习记录
 * @author: zhangshihao
 * @date: 2018-04-11
 */
public class ExerciseRecordActivity extends NavigationLoseActivity {

    private static final String TAG = ExerciseRecordActivity.class.getSimpleName();

    private RecyclerView recyclerView;
    private RelativeLayout rlHeader;
    private LinearLayout llEmptyState;
    private TextView tvHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_record);
        setTitle("我的练习记录");
        initView();
        getData();
    }

    private void initView(){
        recyclerView = findViewById(R.id.lv_exercise_record);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        rlHeader = findViewById(R.id.rl_header);
        llEmptyState = findViewById(R.id.ll_empty_state);
        llEmptyState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
        tvHint = findViewById(R.id.tv_hint);
    }

    private void getData(){
        int child_id = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.EXERCISE_RECORD_NEW);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id",child_id);
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<ExerciseRecordTotalBean>() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(ExerciseRecordTotalBean exerciseRecordTotalBean) {
                        if(isRecordTotalBeanNoData(exerciseRecordTotalBean)){
                            rlHeader.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.GONE);
                            llEmptyState.setVisibility(View.VISIBLE);
                            tvHint.setText("暂无练习记录,可点击重试");
                        }else{
                            rlHeader.setVisibility(View.VISIBLE);
                            recyclerView.setVisibility(View.VISIBLE);
                            llEmptyState.setVisibility(View.GONE);
                            if(exerciseRecordTotalBean != null){
                                List<ExerciseRecordListBean> recordListBeans = exerciseRecordTotalBean.exerciseRecordListBeans;
                                List<ExerciseRecordListBean> tempListBeans = new ArrayList<>();
                                tempListBeans.addAll(recordListBeans);
                                for(int i=0;i<tempListBeans.size();i++){
                                    ExerciseRecordListBean listBean = tempListBeans.get(i);
                                    if(listBean.exercise_num <= 0){
                                        recordListBeans.remove(listBean);
                                    }
                                }
                                ExerciseRecordListAdapter adapter = new ExerciseRecordListAdapter(ExerciseRecordActivity.this,recordListBeans);
                                adapter.setOnMoreClickListener(new ExerciseRecordListAdapter.OnMoreClickListener() {
                                    @Override
                                    public void onClick(String practice_type) {
                                        Intent intent = new Intent(ExerciseRecordActivity.this,ExerciseRecordDetailsActivity.class);
                                        intent.putExtra("practice_type",practice_type);
                                        startActivityAllow(intent);
                                    }
                                });
                                adapter.setOnLastRecordClickListener(new ExerciseRecordListAdapter.OnLastRecordClickListener() {
                                    @Override
                                    public void onLastRecordClick(int practiceType, LastExerciseRecordBean lastBean) {
                                        Intent intent = new Intent(ExerciseRecordActivity.this,NewRecordResultActivity.class);
                                        intent.putExtra("paper_id",lastBean.paper_id);
                                        intent.putExtra("create_time",lastBean.create_time);
                                        intent.putExtra("practice_type",String.valueOf(practiceType));
                                        startActivityAllow(intent);
                                    }
                                });
                                recyclerView.setAdapter(adapter);
                            }
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                        rlHeader.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.GONE);
                        llEmptyState.setVisibility(View.VISIBLE);
                        tvHint.setText("当前网络缓慢，请点击重试");
                    }
                });
    }

    private boolean isRecordTotalBeanNoData(ExerciseRecordTotalBean exerciseRecordTotalBean){
        if(exerciseRecordTotalBean.syncListBean.exercise_num == 0
                && exerciseRecordTotalBean.smartListBean.exercise_num == 0
                && exerciseRecordTotalBean.learnByDoListBean.exercise_num == 0
                && exerciseRecordTotalBean.easyWrongListBean.exercise_num == 0
                && exerciseRecordTotalBean.keyPointListBean.exercise_num == 0
                && exerciseRecordTotalBean.difficultListBean.exercise_num == 0
                && exerciseRecordTotalBean.specialListBean.exercise_num == 0
                && exerciseRecordTotalBean.strangeWordsListBean.exercise_num == 0
                && exerciseRecordTotalBean.speakingListBean.exercise_num == 0
                && exerciseRecordTotalBean.englishWordsListBean.exercise_num == 0
                && exerciseRecordTotalBean.hearingListBean.exercise_num == 0){
            return true;
        }
        return false;
    }

}
